<?php
require("../../../../../libraries/config.php");
require("../../../../../libraries/dbclass2.php");
require("../../../../../libraries/function.php");
require("../../../../../libraries/cache.php");
//Xcrud remove event ----------------
function xcrud_remove_post($id){
	$db=new db;
	$db->connect();
	$db->query('select * from index_post where id='.$id);
	$rc=$db->fetchOne();
		$pid=$rc['post_id'];
	if($rc['is_image']==1){
	
		$db->nonquery('delete from images where post_id='.$pid);
		$db->nonquery('delete from post_image where id='.$pid);
	}else
	if($rc['is_video']==1){
		$db->nonquery('delete from post_media where id='.$pid);
	}else
	if($rc['is_text']==1){
		$db->nonquery('delete from post_text where id='.$pid);
	}
	$db->close();


}

function xcrud_remove_post_image($id){
	$db=new db;
	$db->connect();
	

	
	$db->nonquery('delete from images where post_id='.$id);
	$db->nonquery('delete from index_post where post_id='.$id.' and is_image=1');
	$db->close();



}
function xcrud_remove_post_video($id){
	$db=new db;
	$db->connect();
	$db->nonquery('delete from index_post where is_video=1 and post_id='.$id);
	$db->close();



}
function xcrud_remove_post_text($id){
	$db=new db;
	$db->connect();
	$db->nonquery('delete from index_post where is_text=1 and post_id='.$id);
	$db->close();



}
function xcrud_remove_truyen($id){
	//Rmove truyen,truyen_chuong_link,remove truyenchuong,remove truyen_chuong_noidung,remove_truyen_tacgia_fk,remove truyen_theloai_fk
	$db=new db;
	$db->connect();
	$db->nonquery('delete from truyen where id='.$id);
	$db->nonquery('delete from truyen_chuong_link where story_id='.$id);
	$db->nonquery('delete from truyen_chuong where truyen_id='.$id);
	
	$db->nonquery('delete from truyen_chuong_noidung where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung1 where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung2 where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung3 where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung4 where truyen_id='.$id);	
	$db->nonquery('delete from truyen_chuong_noidung5 where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung6 where truyen_id='.$id);	
	$db->nonquery('delete from truyen_chuong_noidung7 where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung8 where truyen_id='.$id);	
	$db->nonquery('delete from truyen_chuong_noidung9 where truyen_id='.$id);
	$db->nonquery('delete from truyen_chuong_noidung10 where truyen_id='.$id);				
	
	$db->nonquery('delete from truyen_tacgia_fk where truyen_id='.$id);
	$db->nonquery('delete from truyen_theloai_fk where truyen_id='.$id);
	$db->nonquery('delete from truyen_tag_fk where truyen_id='.$id);
	$db->nonquery('delete from truyensearch where id='.$id);
	$db->close();



}
// End Xcrud remove event ----------------

//Xcrud -update event
function xcrud_update_post_text($data,$primary,$xcrud){
	
	$data->set('alias',khongdau($data->get('title')));
	
	$db=new db;
	$db->connect();
	if($data->get('sub_id')){
		$sub_id=intval($data->get('sub_id'));
	}else{
		$sub_id='NULL';
	}
	if($data->get('album_id')){
		$album_id=intval($data->get('album_id'));
	}else{
		$album_id='NULL';
	}
	$sql="update index_post set title='".mysql_real_escape_string($data->get('title'))."',cat_id=".$data->get('cat_id').",sub_id=".$sub_id.",album_id=".$album_id." where post_id=".$primary." and is_text=1";
	$db->query($sql);
	$db->query("select id from index_post where is_text=1 and post_id=".$primary);
	$r1=$db->fetchOne();
	
	$db->close();
	
	$cache=new cache;
	$cache->flush($r1['id'],'post/post_text');
	
}

function xcrud_update_post_video($data,$primary,$xcrud){
	
	$data->set('alias',khongdau($data->get('title')));
	
	$db=new db;
	$db->connect();
	if($data->get('sub_id')){
		$sub_id=intval($data->get('sub_id'));
	}else{
		$sub_id='NULL';
	}
	if($data->get('album_id')){
		$album_id=intval($data->get('album_id'));
	}else{
		$album_id='NULL';
	}
	$sql="update index_post set title='".mysql_real_escape_string($data->get('title'))."',cat_id=".intval($data->get('cat_id')).",sub_id=".$sub_id.",album_id=".$album_id." where post_id=".$primary." and is_video=1";
	$db->query($sql);
	$db->close();
	$cache=new cache;
	$cache->flush($primary,'post/post_media');
	
}

function xcrud_update_post_image($data,$primary,$xcrud){

	$data->set('alias',khongdau($data['post_image.title']));
	
	$db=new db;
	$db->connect();
	if($data->get('sub_id')){
		$sub_id=intval($data->get('sub_id'));
	}else{
		$sub_id='NULL';
	}
	if($data->get('album_id')){
		$album_id=intval($data->get('album_id'));
	}else{
		$album_id='NULL';
	}
	$sql="update index_post set title='".mysql_real_escape_string($data->get('title'))."',cat_id=".intval($data->get('cat_id')).",sub_id=".$sub_id.",album_id=".$album_id." where post_id=".$primary." and is_image=1";
	$db->query($sql);
	$db->close();
	$cache=new cache;
	$cache->flush($primary,'post/post_media');
	
}
//End of xcrud -update event

//Xcrud truyen event ---------------------
function check_chuong_noidung($data, $primary, $xcrud){
		
	//Kiem tra chuong co image ko, neu co image thi set image = 1 va curl de download image ve-----
	//Kiem tra thu tu chuong
	//Kiem tra permission, user level 4 thi chi duoc quyen update chuong cua minh, con lai duoc quyen update het
	
	//Kiem tra image có không
	$j=check_image($data->get('noidung'));
	if($j>0){
		$url="content=".urlencode($data->get('noidung'))."&name=mong.vn";	
		$data->set('noidung',urldecode(mycurl($url,'http://sv1.mong.vn/mong/images/story/story.php')));
	
	}
	


}

function check_truyen_chuong($data, $primary, $xcrud){
	
	
	$data->set('alias',khongdau(trim($data->get('ten'))));
	
	//Kiem tra thu tu chuong
	$db=new db;
	$db->connect();
	$sql1='select * from truyen_chuong where id='.$primary;
	$db->query($sql1);
	$row=$db->fetchOne();
	//Valid pessmion
	if($_SESSION['user']['level']==4){
		if($_SESSION['user']['username']!=$row['user_upload']){
			
			$db->query("select user_upload from truyen where id=".$row['truyen_id']);
			$row2=$db->fetchOne();
			if($row2['user_upload']==$_SESSION['user']['username']){
			}else{
				 $xcrud->set_exception('truyen_id',' Bạn không có quyền cập nhật chương truyện này !');
				 $db->close();
				 exit();
			}
			//Kiểm tra truyện này có phải user add không -----
		}
	}
	$sql="select id from truyen_chuong where id !=".$primary." and truyen_id=".$row['truyen_id']." and thutu =".floatval($data->get('thutu'));
	$db->query($sql);
	if($db->num_rows()>0){
	 $xcrud->set_exception('thutu','Thứ tự này bị trùng với chương khác cùng truyện, xin vui lòng kiểm tra lại !');
	}
	
	$db->close();
	//Kiem tra permission, user level 4 thi chi duoc quyen update chuong cua minh, con lai duoc quyen update het
	//Kiem tra image có không
	
	

}

//Xcrud check the loai -----
function check_theloai_update($data,$primary,$xcrud){
	truyen_theloai($primary,$data->get('theloai'));
	truyen_tag($primary,$data->get('tags'));
	truyen_tacgia($primary,$data->get('tacgia'));
	$data->set('alias',khongdau(trim($data->get('ten'))));
	$db=new db;
	$db->connect();
	$db->query("update truyensearch set ten='".mysql_real_escape_string($data->get('ten'))."',alias='".$data->get('alias')."' where id=".$primary);

	$db->close();
	
	//Clear cache truyen
	$cache =new cache;
	$cache->flush($primary,'truyen/'.$primary);
	
}

function check_theloai_insert($data,$primary,$xcrud){
	truyen_theloai($primary,$data->get('theloai'));
	truyen_tag($primary,$data->get('tags'));
	truyen_tacgia($primary,$data->get('tacgia'));
	$data->set('alias',khongdau(trim($data->get('ten'))));
	$db=new db;
	$db->connect();
	$db->query("insert into truyensearch(id,ten,alias) value($primary,'".mysql_real_escape_string($data->get('ten'))."','".$data->get('alias')."')");
	
	$db->close();
	
	
}

function check_truyen_alias($data,$xcrud){
	//Kiem tra ten truyen co bi trung ko
	$db=new db;
	$db->connect();
	$db->query("select id from truyen where ten = '".trim($data->get('ten'))."'");
	if($db->num_rows()>0){
	 	$xcrud->set_exception('tentruyen',' Truyện này đã tồn tại rồi, xin vui lòng kiểm tra lại. Nếu  cần hỗ trợ xin vui lòng liên hệ ban quản trị Mong.vn!');
		
	}else{
	$data->set('alias',khongdau(trim($data->get('ten'))));
	}
	
}
// Xcrud end tags
function check_image($data){
$data = mb_convert_encoding($data, 'HTML-ENTITIES', "UTF-8"); 
$dom = new DOMDocument;
 $dom->loadHTML($data); 
 $sxe = simplexml_import_dom($dom);

	$j=0;	
	 foreach ($sxe->xpath('//img[@src]') as $a)
	 {	
		
		$j++;
	 }
 return $j;
 }
 
?>
